<?php

/**
 * media actions.
 *
 * @package    vanilla
 * @subpackage media
 * @author     Emiliano Nobis <emiliano.nobis@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class mediaActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
			$prefix = $request->getParameter('prefix');
      		$field = $request->getParameter('field');
			if($prefix != '')      
				$this->getUser()->setAttribute('prefix', $prefix);
			if($field != '')
      	$this->getUser()->setAttribute('field', $field);
      return $this->renderComponent('media', 'list');
  }

  public function executeUpload(sfWebRequest $request) {
      $this->setLayout('mediaUpLayout');
      $this->form = new VanillaMediaImageForm();

      if($request->isMethod('post')) {
          $this->form->bind($request->getParameter('vanilla_media_image'), $request->getFiles('vanilla_media_image'));

          if($this->form->isValid()) {
              $file = $this->form->getValue('img');
              $filename = $file->getOriginalName();
              $file->save(sfConfig::get('sf_upload_dir').'/media/'.$filename);

              $thumb = array('dir' => 'thumb', 'width' => '200', 'height' => '200');

              $thumbnail = new sfThumbnail($thumb['width'], $thumb['height'], true, false);
              $thumbnail->loadFile(sfConfig::get('sf_upload_dir')."/media/".$filename);
              $thumbnail->save(sfConfig::get('sf_upload_dir')."/media/".$thumb['dir']."/".$filename, 'image/jpeg');

              $vmi = new VanillaMediaImage();
              $vmi->setImg($filename);
              $it = $this->form->getValue('it');
              $en = $this->form->getValue('en');
              $vmi->setTitle($it['title'], 'it');
              $vmi->setTitle($en['title'], 'en');
              $vmi->save();
          }
      }
  }

  public function executeDelete(sfWebRequest $request) {
      $criteria = new Criteria();

      $this->id = $request->getParameter('picid');
      $criteria->add(VanillaMediaImagePeer::ID, $this->id);

      $vmi = VanillaMediaImagePeer::doSelectOne($criteria);

      $vmi->delete();

      return $this->renderComponent('media', 'list');
  }
}
